﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebUI.DAL;
using System.Data.Linq;

namespace WebUI.BL
{
    public class ProductKeyWordsBL
    {
        public void Insert(tbl_ProductKeyWord productKeyWord)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_ProductKeyWord newproductKeyWord = new tbl_ProductKeyWord()
                    {
                        Status = productKeyWord.Status,
                        ProductNo = productKeyWord.ProductNo,
                        Link = productKeyWord.Link,
                        KeyWordVN = productKeyWord.KeyWordVN,
                        KeyWordName = productKeyWord.KeyWordName
                    };
                    contextdb.tbl_ProductKeyWords.InsertOnSubmit(newproductKeyWord);
                    contextdb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void Update(tbl_ProductKeyWord productKeyWord)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var updateproductKeyWord = contextdb.tbl_ProductKeyWords.SingleOrDefault(u => u.KeyWordNo == productKeyWord.KeyWordNo);
                try
                {
                    if (updateproductKeyWord != null)
                    {
                        updateproductKeyWord.Status = productKeyWord.Status;
                        updateproductKeyWord.ProductNo = productKeyWord.ProductNo;
                        updateproductKeyWord.Link = productKeyWord.Link;
                        updateproductKeyWord.KeyWordVN = productKeyWord.KeyWordVN;
                        updateproductKeyWord.KeyWordName = productKeyWord.KeyWordName;
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }

            }
        }
        public void Delete(int productKeyWordNo)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteProductKeyWord = contextdb.tbl_ProductKeyWords.SingleOrDefault(u => u.KeyWordNo == productKeyWordNo);
                try
                {
                    if (deleteProductKeyWord != null)
                    {
                        contextdb.tbl_ProductKeyWords.DeleteOnSubmit(deleteProductKeyWord);
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void DeleteByStatus(int productKeyWordNo)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteProductKeyWord = contextdb.tbl_ProductKeyWords.SingleOrDefault(u => u.KeyWordNo == productKeyWordNo);
                try
                {
                    if (deleteProductKeyWord != null)
                    {
                        deleteProductKeyWord.Status = "Deleted";
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }

        public IEnumerable<tbl_ProductKeyWord> GetProductkeyWord(string keyword)
        {
            var dbcontext = new AdlerContextDataContext();
            //return dbcontext.tbl_ProductKeyWords;
            return dbcontext.tbl_ProductKeyWords.Where(x => x.KeyWordName.Contains(keyword) || x.KeyWordVN.Contains(keyword)).AsEnumerable();
        }
    }
}
